﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WTwitter.Model.Framework;

namespace WTwitter.ViewModel {
	sealed public class StatusBarViewModel : ViewModelBase {
		#region private member
		private string _text;
		private string _recentMessages;
		private List<string> _resentMessagesList = new List<string>();
		private const int _recentNumMax = 10;
		private MainWindowViewModel _mainWindow;
		#endregion

		public StatusBarViewModel(MainWindowViewModel mainWindow) 
			: base("") {
			_mainWindow = mainWindow;
			Message.MessageAdded += this.OnMessageAdded;
		}

		#region Command
		private RelayCommand _optionDialogCommmand;
		/// <summary>
		/// オプションダイアログを開く
		/// </summary>
		public ICommand OptionDialogCommand {
			get {
				if (_optionDialogCommmand == null) {
					_optionDialogCommmand = new RelayCommand(_mainWindow.OpenOptionDialog);
				}
				return _optionDialogCommmand;
			}
		}
		#endregion

		/// <summary>
		/// 一番最新のメッセージを表す文字列
		/// </summary>
		public string Text {
			set {
				if (_text != value) {
					_text = value;
					OnPropertyChanged("Text");
				}
			}

			get {
				return _text;
			}
		}

		/// <summary>
		/// 最近のメッセージを表す文字列（改行で区切られた１つの文字列）
		/// </summary>
		public string RecentMassages {
			set {
				if (_recentMessages != value) {
					_recentMessages = value;
					OnPropertyChanged("RecentMassages");
				}

			}
			get {
				return _recentMessages;
			}
		}

		private void OnMessageAdded(object sender, MessageAddedEventArgs e) {
			string text = string.Format("[{0}] {1}", e.Item.Time.ToLocalTime().ToString("HH:mm"), e.Item.Message);
			Text = text;

			_resentMessagesList.Add(text);
			if (_resentMessagesList.Count > _recentNumMax) {
				_resentMessagesList.RemoveAt(0);
			}
			RecentMassages = string.Join(Environment.NewLine, _resentMessagesList.ToArray());
		}
	}
}
